package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.lala.rennigou.pojo.*;
import com.lala.rennigou.service.EasybuyOrderDetailService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class EasybuyOrderDetailController {
    @Autowired
    private EasybuyOrderDetailService easybuyOrderDetailService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    public EasybuyUser yanzheng(HttpServletRequest request){
        EasybuyUser easybuyUser=null;
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    if(map!=null){
                        easybuyUser=(EasybuyUser)map.get("user");
                    }
                }
            }
        }
        return easybuyUser;
    }
    @RequestMapping("/removeGWC")
    private Object removeGWC(String out,String address,String prices,HttpServletRequest request){

        Map<String ,Object> mapp=new HashMap<>();
        EasybuyUser easybuyUser=yanzheng(request);
        Map<String ,Object> map=(Map<String ,Object>)redisTemplate.opsForValue().get(easybuyUser.getLoginname()+"la");
        List<EasybuyProduct> list=(List<EasybuyProduct>)map.get("list");
//        Map<Object,Object> m=(Map<Object,Object>)map.get("num");
        EasybuyOrder easybuyOrder=new EasybuyOrder();
        easybuyOrder.setUseraddress(address);
        easybuyOrder.setUserid(easybuyUser.getId());
        easybuyOrder.setLoginname(easybuyUser.getLoginname());
        easybuyOrder.setCost(Float.parseFloat(prices));
        easybuyOrder.setSerialnumber(out);
        easybuyOrder.setCreatetime(new Date());
//        Float f=0f;
//        for (EasybuyProduct e:list){
//            f+=e.getNum()*e.getPrice();
//        }
       int a= easybuyOrderDetailService.la(easybuyOrder,list,easybuyUser.getId());
        if (a==-1){
            mapp.put("la",1);
            mapp.put("msg","付款失败，请与管理员联系！");
            return JSONArray.toJSON(mapp);
        }else if (a==-2){
            mapp.put("la",1);
            mapp.put("msg","库存不足！");
            return JSONArray.toJSON(mapp);
        }
        redisTemplate.delete(easybuyUser.getLoginname()+"la");
        mapp.put("la",2);
        mapp.put("zong",prices);
        mapp.put("dd",easybuyOrder.getSerialnumber());
        return JSONArray.toJSON(mapp);

    }

    @RequestMapping("/orderdetail")
    private Object orderdetail(int orderid){
        return "";
    }


}
